<?php
/**
 * Created by PhpStorm.
 * User: tangchao
 * Date: 19-4-20
 * Time: 下午2:58
 */

namespace Lib;


//原型模式 演示-画矩形
class Canvas
{
    public $data;
    private $decorators = [];

    function init($width = 20, $height = 10){
        $data = [];
        for ($i = 0; $i < $height; $i++){
            for ($j = 0;$j < $width; $j++){
                $data[$i][$j] = '*';
            }
        }
        $this->data = $data;
    }

    function addDecorator(DrawDec $decorator){
        $this->decorators[] = $decorator;
    }

    function before(){
        foreach ($this->decorators as $decorator) {
            $decorator->before();
        }
    }

    function after(){
        $decorators = array_reverse($this->decorators);
        foreach ($decorators as $decorator) {
            $decorator->after();
        }
    }


   function draw(){
        $this->before();
       foreach ($this->data as $line) {
           foreach ($line as $char) {
               echo $char;
           }
           echo PHP_EOL;
        }
        $this->after();
   }

   function rect($a1,$a2,$b1,$b2){
       foreach ($this->data as $k1 => $line) {
           if ($k1 < $a1 || $k1 > $a2) continue;
           foreach ($line as $k2 => $char) {
               if ($k2 < $b1 || $k2 > $b2) continue;
               $this->data[$k1][$k2] = " ";
           }
        }
   }
}